// 国际化翻译配置
const translations = {
    zh: {
        // 通用
        'app.name': 'OpenOKR',
        'app.subtitle': '开放式目标与关键成果管理系统',
        'common.save': '保存',
        'common.cancel': '取消',
        'common.delete': '删除',
        'common.edit': '编辑',
        'common.create': '创建',
        'common.close': '关闭',
        'common.confirm': '确认',
        'common.search': '搜索',
        'common.filter': '筛选',
        'common.all': '全部',
        'common.loading': '加载中...',
        'common.success': '操作成功',
        'common.error': '操作失败',
        'common.yes': '是',
        'common.no': '否',

        // 登录页面
        'login.title': '登录',
        'login.username': '用户名',
        'login.password': '密码',
        'login.submit': '登录',
        'login.welcome': '欢迎回来',

        // 导航栏
        'nav.dashboard': '仪表板',
        'nav.objectives': '目标',
        'nav.projects': '项目',
        'nav.team': '团队',
        'nav.admin': '管理员',
        'nav.logout': '退出登录',
        'nav.profile': '个人资料',

        // 仪表板
        'dashboard.manager': '主管仪表板',
        'dashboard.employee': '员工仪表板',
        'dashboard.admin': '管理员仪表板',
        'dashboard.welcome': '欢迎',
        'dashboard.overview': '概览',
        'dashboard.statistics': '统计',

        // 目标相关
        'objective.title': '目标',
        'objective.department': '部门目标',
        'objective.personal': '个人目标',
        'objective.company': '公司目标',
        'objective.create': '创建目标',
        'objective.edit': '编辑目标',
        'objective.delete': '删除目标',
        'objective.delete.confirm': '确定要删除这个目标吗？此操作不可恢复。',
        'objective.title.label': '目标标题',
        'objective.description': '目标描述',
        'objective.type': '目标类型',
        'objective.status': '状态',
        'objective.progress': '进度',
        'objective.start_date': '开始日期',
        'objective.end_date': '结束日期',
        'objective.owner': '负责人',
        'objective.parent': '父目标',
        'objective.children': '子目标',
        'objective.keyresults': '关键成果',
        'objective.business': '经营',
        'objective.strategy': '战略',
        'objective.development': '研发',
        'objective.operations': '运营',
        'objective.subordinates': '下属认领',
        'objective.projects': '关联项目',
        'objective.subordinate_list': '下属认领的目标',
        'objective.project_list': '关联的项目',

        // 项目相关
        'project.title': '项目',
        'project.create': '创建项目',
        'project.edit': '编辑项目',
        'project.delete': '删除项目',
        'project.delete.confirm': '确定要删除这个项目吗？',
        'project.name': '项目名称',
        'project.description': '项目描述',
        'project.owner': '负责人',
        'project.assign_owner': '分配负责人',
        'project.status': '状态',
        'project.progress': '进度',
        'project.start_date': '开始日期',
        'project.end_date': '结束日期',
        'project.objective': '服务目标',
        'project.select_objective': '选择此项目服务的目标',
        'project.unassigned': '未分配',

        // 状态
        'status.active': '进行中',
        'status.completed': '已完成',
        'status.pending': '待开始',
        'status.archived': '已归档',

        // 团队
        'team.title': '团队',
        'team.members': '团队成员',
        'team.subordinates': '我的下属',
        'team.manager': '上级',

        // AI查询
        'ai.query': 'AI智能查询',
        'ai.placeholder': '输入你的问题，例如：我的团队本周进展如何？',
        'ai.send': '发送',
        'ai.thinking': 'AI思考中...',
        'ai.error': '查询出错，请稍后重试',

        // 用户详情
        'user.detail': '用户详情',
        'user.username': '用户名',
        'user.fullname': '姓名',
        'user.email': '邮箱',
        'user.role': '角色',
        'user.department': '部门',
        'user.manager': '上级',
        'user.objectives': '目标列表',
        'user.projects': '项目列表',

        // 角色
        'role.admin': '管理员',
        'role.manager': '主管',
        'role.employee': '员工',

        // 统计
        'stats.total_objectives': '总目标数',
        'stats.active_objectives': '进行中',
        'stats.completed_objectives': '已完成',
        'stats.avg_progress': '平均进度',
        'stats.total_projects': '总项目数',
        'stats.team_members': '团队成员',
    },
    en: {
        // Common
        'app.name': 'OpenOKR',
        'app.subtitle': 'Open Objectives and Key Results Management System',
        'common.save': 'Save',
        'common.cancel': 'Cancel',
        'common.delete': 'Delete',
        'common.edit': 'Edit',
        'common.create': 'Create',
        'common.close': 'Close',
        'common.confirm': 'Confirm',
        'common.search': 'Search',
        'common.filter': 'Filter',
        'common.all': 'All',
        'common.loading': 'Loading...',
        'common.success': 'Success',
        'common.error': 'Error',
        'common.yes': 'Yes',
        'common.no': 'No',

        // Login page
        'login.title': 'Login',
        'login.username': 'Username',
        'login.password': 'Password',
        'login.submit': 'Login',
        'login.welcome': 'Welcome Back',

        // Navigation
        'nav.dashboard': 'Dashboard',
        'nav.objectives': 'Objectives',
        'nav.projects': 'Projects',
        'nav.team': 'Team',
        'nav.admin': 'Admin',
        'nav.logout': 'Logout',
        'nav.profile': 'Profile',

        // Dashboard
        'dashboard.manager': 'Manager Dashboard',
        'dashboard.employee': 'Employee Dashboard',
        'dashboard.admin': 'Admin Dashboard',
        'dashboard.welcome': 'Welcome',
        'dashboard.overview': 'Overview',
        'dashboard.statistics': 'Statistics',

        // Objectives
        'objective.title': 'Objectives',
        'objective.department': 'Department Objectives',
        'objective.personal': 'Personal Objectives',
        'objective.company': 'Company Objectives',
        'objective.create': 'Create Objective',
        'objective.edit': 'Edit Objective',
        'objective.delete': 'Delete Objective',
        'objective.delete.confirm': 'Are you sure you want to delete this objective? This action cannot be undone.',
        'objective.title.label': 'Objective Title',
        'objective.description': 'Description',
        'objective.type': 'Type',
        'objective.status': 'Status',
        'objective.progress': 'Progress',
        'objective.start_date': 'Start Date',
        'objective.end_date': 'End Date',
        'objective.owner': 'Owner',
        'objective.parent': 'Parent Objective',
        'objective.children': 'Child Objectives',
        'objective.keyresults': 'Key Results',
        'objective.business': 'Business',
        'objective.strategy': 'Strategy',
        'objective.development': 'Development',
        'objective.operations': 'Operations',
        'objective.subordinates': 'Subordinate Claims',
        'objective.projects': 'Related Projects',
        'objective.subordinate_list': 'Objectives Claimed by Subordinates',
        'objective.project_list': 'Related Projects',

        // Projects
        'project.title': 'Projects',
        'project.create': 'Create Project',
        'project.edit': 'Edit Project',
        'project.delete': 'Delete Project',
        'project.delete.confirm': 'Are you sure you want to delete this project?',
        'project.name': 'Project Name',
        'project.description': 'Description',
        'project.owner': 'Owner',
        'project.assign_owner': 'Assign Owner',
        'project.status': 'Status',
        'project.progress': 'Progress',
        'project.start_date': 'Start Date',
        'project.end_date': 'End Date',
        'project.objective': 'Linked Objective',
        'project.select_objective': 'Select objective this project serves',
        'project.unassigned': 'Unassigned',

        // Status
        'status.active': 'Active',
        'status.completed': 'Completed',
        'status.pending': 'Pending',
        'status.archived': 'Archived',

        // Team
        'team.title': 'Team',
        'team.members': 'Team Members',
        'team.subordinates': 'My Subordinates',
        'team.manager': 'Manager',

        // AI Query
        'ai.query': 'AI Query',
        'ai.placeholder': 'Ask a question, e.g.: How is my team progressing this week?',
        'ai.send': 'Send',
        'ai.thinking': 'AI is thinking...',
        'ai.error': 'Query failed, please try again later',

        // User Details
        'user.detail': 'User Details',
        'user.username': 'Username',
        'user.fullname': 'Full Name',
        'user.email': 'Email',
        'user.role': 'Role',
        'user.department': 'Department',
        'user.manager': 'Manager',
        'user.objectives': 'Objectives',
        'user.projects': 'Projects',

        // Roles
        'role.admin': 'Admin',
        'role.manager': 'Manager',
        'role.employee': 'Employee',

        // Statistics
        'stats.total_objectives': 'Total Objectives',
        'stats.active_objectives': 'Active',
        'stats.completed_objectives': 'Completed',
        'stats.avg_progress': 'Avg Progress',
        'stats.total_projects': 'Total Projects',
        'stats.team_members': 'Team Members',
    }
};

// 当前语言
let currentLang = localStorage.getItem('language') || 'zh';

// 翻译函数
function t(key) {
    return translations[currentLang][key] || key;
}

// 切换语言
function switchLanguage(lang) {
    currentLang = lang;
    localStorage.setItem('language', lang);
    updatePageLanguage();
}

// 更新页面语言
function updatePageLanguage() {
    // 更新所有带有 data-i18n 属性的元素
    document.querySelectorAll('[data-i18n]').forEach(el => {
        const key = el.getAttribute('data-i18n');
        el.textContent = t(key);
    });

    // 更新所有带有 data-i18n-placeholder 属性的元素
    document.querySelectorAll('[data-i18n-placeholder]').forEach(el => {
        const key = el.getAttribute('data-i18n-placeholder');
        el.placeholder = t(key);
    });

    // 更新所有带有 data-i18n-title 属性的元素
    document.querySelectorAll('[data-i18n-title]').forEach(el => {
        const key = el.getAttribute('data-i18n-title');
        el.title = t(key);
    });

    // 更新页面标题
    const titleKey = document.querySelector('meta[name="page-title"]');
    if (titleKey) {
        document.title = t(titleKey.content);
    }

    // 触发自定义事件，允许页面进行额外的更新
    document.dispatchEvent(new CustomEvent('languageChanged', { detail: { lang: currentLang } }));
}

// 获取类型标签的翻译
function getTypeLabel(type) {
    const typeMap = {
        'operational': 'objective.business',
        'strategic': 'objective.strategy',
        'development': 'objective.development'
    };
    return t(typeMap[type] || type);
}

// 获取状态的翻译
function getStatusLabel(status) {
    const statusMap = {
        'active': 'status.active',
        'completed': 'status.completed',
        'pending': 'status.pending',
        'archived': 'status.archived'
    };
    return t(statusMap[status] || status);
}

// 页面加载时初始化语言
document.addEventListener('DOMContentLoaded', () => {
    updatePageLanguage();
});
